/**
 * Created by chengming on 18/5/27.
 * version 1.0
 */
const JsonAdapter = require('../JsonAdapter');
const iconv = require("iconv-lite");
const vm = require('vm');
const request = require('request-promise');
const logger = require('../../conf/logger');
const urlTool = require('url');
const quaryString = require('querystring');
const urlencode = require("urlencode")
const RandomIp = require("../../util/RandomIp")

/**
 * 北京互金网
 * @type {module.Bjp2p}
 */
module.exports = class ZhaoShangQiHuoPostJson extends JsonAdapter{

    /*async doRequest(){
        let ip = "";
        ip += RandomIp(11,191) +".";
        ip += RandomIp(11,191) +".";
        ip += RandomIp(11,191) +".";
        ip += RandomIp(11,191) +"";
        let options = this.getRequestOptions();
        // options.host="139.129.6.138";
        // options.port="8333";
        // options.username="jansun";
        // options.password="jan321"
        // options.host="115.29.176.27";
        // options.port="6688";
        // options.username="d5849ef1d";
        // options.password="cb1110c77"
        options.headers['Host']='feed.mix.sina.com.cn';
        options.headers['Accept']='*!/!*';
        options.headers['Accept-Language']='zh-CN,zh;q=0.9';

       // options.headers['Proxy-Authentication']='Base ' + new Buffer('jansun:jan@#$!').toString('base64')
        logger.debug(this.pageName +" 的请求头信息为" + JSON.stringify(options));
        var proxyUrl = "http://jansun:jan321@139.129.6.138:8333";
        var proxyRequest = await request.default({'proxy':proxyUrl});
        return await proxyRequest(options);
    }*/

    async reqJson(){
        let query = urlTool.parse(this.requestUrl).query;
        let callbackFun = quaryString.parse(query).callback;

        const body = await this.doRequest();
        console.log('--------body: ' + body)
        let decodeBody = iconv.decode(body, this.encoding).toString();
        decodeBody=decodeBody.replace('try{parent.jsonpCallback(','');
        decodeBody=decodeBody.replace(');}catch(e){};','');

        let data = JSON.parse(decodeBody);

        return data;
	}
}
